#include "Player.hpp"
#include <iostream>

Player::Player() {
    shape.setSize({ 50.f, 50.f });
    shape.setFillColor(sf::Color::Green);
    shape.setPosition(100.f, 100.f);
    velocity = { 0.f, 0.f };
}

void Player::handleInput() {
    velocity = { 0.f, 0.f };

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) velocity.y = -speed;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) velocity.y = speed;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) velocity.x = -speed;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) velocity.x = speed;

    if (velocity.x != 0 && velocity.y != 0) {
        velocity *= 0.7071f;
    }
}

void Player::update(float dt) {
    handleInput();
    sf::Vector2f newPos = shape.getPosition() + velocity * dt;

    newPos.x = std::max(0.f, std::min(newPos.x, 800.f - shape.getSize().x));
    newPos.y = std::max(0.f, std::min(newPos.y, 600.f - shape.getSize().y));

    shape.setPosition(newPos);
}

void Player::render(sf::RenderWindow& window) {
    window.draw(shape);
}

sf::Vector2f Player::getPosition() const {
    return shape.getPosition();
}

void Player::move(float dx, float dy) {
    shape.move(dx, dy);
}